/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.DAO;


import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import transervfx.Dato.Entidad.Localidad;
import transervfx.Dato.Entidad.Pais;
import transervfx.Dato.Entidad.Provincia;
import transervfx.Dato.Entidad.ZonaRegional;

/**
 *
 * @author oscar
 */
public class PaisDAO implements Dao<Pais,Long> {
   
    private static PaisDAO INSTANCE = null;
    
    
     /**
     *  Crea el Gestor Auxiliar
     */
    protected PaisDAO() {
    }

    /**
     * creador sincronizado para protegerse de posibles problemas  multi-hilo
     * otra prueba para evitar instanciación múltiple
     */
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new PaisDAO();
        }
    }

    /**
     * Obtiene la instancia.
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static PaisDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }
    

    @Override
    public void persist(Pais entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                em.persist(entity);
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    @Override
     //Sin transaccion
    public void remove(Pais entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            em.remove(entity);

        } finally {
            em.close();
        }
    }
    
     public void  update(Pais bean){
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

          em.getTransaction().begin();
          if(em.contains(bean))
          {
            em.merge(bean);
          }
            em.getTransaction().commit();

        } finally {
            em.close();
        }
       
        
    }

    @Override
    public Pais findById(Long id) {
       EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(Pais.class, id);

        } finally {
            em.close();
        }
    }
    

  
    @Override
    public List<Pais> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("Pais.findAll").getResultList();

        } finally {
            em.close();
        }
    }

   public List<Pais> findByNombre(String nombre) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            Query query = em.createNamedQuery("Pais.findByNombre");
            query.setParameter("nombre", nombre);
           
                return query.getResultList();
            
            
        } finally {
            em.close();
        }
    }
    
    
    
    public List<Provincia> findByProvincias(Pais pais) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
          
           List<Provincia> provincias=em.getReference(Pais.class, pais.getId()).getProvincias();
           provincias.get(0);
                return provincias;
            
            
        } finally {
            em.close();
        }
    }
    
    public List<Localidad> findByLocalidades(Provincia provincia) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
          
            List<Localidad> localidades=em.getReference(Provincia.class, provincia.getId()).getLocalidades();
           localidades.get(0);
                return localidades;
            
            
        } finally {
            em.close();
        }
    }

    public List<ZonaRegional> findByZonas(Localidad localidad) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
          
            List<ZonaRegional> zonasR=em.getReference(Localidad.class, localidad.getId()).getZonasRegionales();
           if(!zonasR.isEmpty()) {
                zonasR.get(0);
            }
                return zonasR;
            
            
        } finally {
            em.close();
        }
    }
    
    

}
